function getQueryObject(url) {
  url = url == null ? window.location.href : url
  const search = url.substring(url.lastIndexOf('?') + 1)
  const obj = {}
  const reg = /([^?&=]+)=([^?&=]*)/g
  search.replace(reg, (rs, $1, $2) => {
    const name = decodeURIComponent($1)
    let val = decodeURIComponent($2)
    val = String(val)
    obj[name] = val
    return rs
  })
  return obj
}
const renderer = new marked.Renderer()
const options = {
  renderer: renderer,
  gfm: true,
  pedantic: false,
  sanitize: false,
  tables: true,
  breaks: false,
  smartLists: true,
  smartypants: false,
  highlight: function (code) {
    return hljs.highlightAuto(code).value
  }
}
function parseMarked(data) {
  return marked.parse(data, options)
}

window.onload = async function () {
  let filePath = getQueryObject().filePath
  if (!filePath) {
    document.body.innerHTML = '文件不存在'
    return
  }
  // 请求类型 type 后台区分用
  const response = await fetch(`/deer${filePath}?type=parseMarked`)
  if (!response.ok) return null
  const res = await response.text()
  if (!res) return null
  const content = document.getElementById('content')
  content.innerHTML = parseMarked(res)
}
